using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Text;

namespace Data_Access_Tier
{
	public class HRM_WORKData : DatabaseObjectBase
	{
		public HRM_WORKData()
		{
			Initialize(true);
		}
		public HRM_WORKData(Boolean newConnection)
		{
			Initialize(newConnection);
		}

		public Int32 HRM_WORK_Insert(Guid WorkID, String EmployeeCode, String Deliverer, String Work, String Priority, DateTime BeginDate, DateTime EndDate, Int32 Status, Double Complete, Decimal Cost, String Description)
		{
			try
			{
				if (_dbconn.State != ConnectionState.Open)
					_dbconn.Open();
				_dbadapter.InsertCommand = new SqlCommand("HRM_WORK_Insert");
				_dbadapter.InsertCommand.CommandType = CommandType.StoredProcedure;
				_dbadapter.InsertCommand.Connection = _dbconn;
				_trans = _dbconn.BeginTransaction();
				_dbadapter.InsertCommand.Transaction = _trans;
				_dbadapter.InsertCommand.Parameters.Clear();
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@WorkID", SqlDbType.UniqueIdentifier));
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@EmployeeCode", SqlDbType.VarChar, 20));
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@Deliverer", SqlDbType.NVarChar, 100));
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@Work", SqlDbType.NVarChar, 255));
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@Priority", SqlDbType.NVarChar, 100));
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@BeginDate", SqlDbType.DateTime));
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@EndDate", SqlDbType.DateTime));
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@Status", SqlDbType.Int));
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@Complete", SqlDbType.Float));
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@Cost", SqlDbType.Money));
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@Description", SqlDbType.NVarChar, 4000));
				_dbadapter.InsertCommand.Prepare();
				SqlGuid WorkIDSQL = WorkID;
				_dbadapter.InsertCommand.Parameters["@WorkID"].Value = WorkIDSQL;
				SqlString EmployeeCodeSQL = EmployeeCode;
				_dbadapter.InsertCommand.Parameters["@EmployeeCode"].Value = EmployeeCodeSQL;
				SqlString DelivererSQL = Deliverer;
				_dbadapter.InsertCommand.Parameters["@Deliverer"].Value = DelivererSQL;
				SqlString WorkSQL = Work;
				_dbadapter.InsertCommand.Parameters["@Work"].Value = WorkSQL;
				SqlString PrioritySQL = Priority;
				_dbadapter.InsertCommand.Parameters["@Priority"].Value = PrioritySQL;
				SqlDateTime BeginDateSQL = SqlDateTime.Null;
				if (BeginDate > DateTime.MinValue)
				{
					try
					{
						BeginDateSQL = BeginDate;
					}
					catch (Exception eBeginDate)
					{
						String BeginDateMessage = eBeginDate.Message;
						BeginDateSQL = SqlDateTime.Null;
					}
				}
				_dbadapter.InsertCommand.Parameters["@BeginDate"].Value = BeginDateSQL;
				SqlDateTime EndDateSQL = SqlDateTime.Null;
				if (EndDate > DateTime.MinValue)
				{
					try
					{
						EndDateSQL = EndDate;
					}
					catch (Exception eEndDate)
					{
						String EndDateMessage = eEndDate.Message;
						EndDateSQL = SqlDateTime.Null;
					}
				}
				_dbadapter.InsertCommand.Parameters["@EndDate"].Value = EndDateSQL;
				SqlInt32 StatusSQL = Status;
				_dbadapter.InsertCommand.Parameters["@Status"].Value = StatusSQL;
				SqlDouble CompleteSQL = Complete;
				_dbadapter.InsertCommand.Parameters["@Complete"].Value = CompleteSQL;
				SqlMoney CostSQL = Cost;
				_dbadapter.InsertCommand.Parameters["@Cost"].Value = CostSQL;
				SqlString DescriptionSQL = Description;
				_dbadapter.InsertCommand.Parameters["@Description"].Value = DescriptionSQL;
				Int32 returnvalue = Convert.ToInt32(_dbadapter.InsertCommand.ExecuteScalar());

				_trans.Commit();
				return returnvalue;
			}
			catch (SqlException e)
			{
				try
				{
					_trans.Rollback();
				}
				catch (SqlException ex)
				{
					throw new Exception("SQL Rollback Error: HRM_WORK_Insert: " + ex.Message, ex);
				}
				throw new Exception("SQL Error: HRM_WORK_Insert: " + e.Message, e);
			}
			finally
			{
				if (_dbadapter.InsertCommand != null)
					_dbadapter.InsertCommand.Dispose();
				CloseConnection();
			}
		}
		public Int32 HRM_WORK_Update(Guid WorkID, String EmployeeCode, String Deliverer, String Work, String Priority, DateTime BeginDate, DateTime EndDate, Int32 Status, Double Complete, Decimal Cost, String Description)
		{
			try
			{
				if (_dbconn.State != ConnectionState.Open)
					_dbconn.Open();
				_dbadapter.UpdateCommand = new SqlCommand("HRM_WORK_Update");
				_dbadapter.UpdateCommand.CommandType = CommandType.StoredProcedure;
				_dbadapter.UpdateCommand.Connection = _dbconn;

				_trans = _dbconn.BeginTransaction();
				_dbadapter.UpdateCommand.Transaction = _trans;

				_dbadapter.UpdateCommand.Parameters.Clear();
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@WorkID", SqlDbType.UniqueIdentifier));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@EmployeeCode", SqlDbType.VarChar, 20));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Deliverer", SqlDbType.NVarChar, 100));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Work", SqlDbType.NVarChar, 255));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Priority", SqlDbType.NVarChar, 100));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@BeginDate", SqlDbType.DateTime));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@EndDate", SqlDbType.DateTime));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Status", SqlDbType.Int));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Complete", SqlDbType.Float));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Cost", SqlDbType.Money));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Description", SqlDbType.NVarChar, 4000));
				_dbadapter.UpdateCommand.Prepare();
				SqlGuid WorkIDSQL = WorkID;
				_dbadapter.UpdateCommand.Parameters["@WorkID"].Value = WorkIDSQL;
				SqlString EmployeeCodeSQL = EmployeeCode;
				_dbadapter.UpdateCommand.Parameters["@EmployeeCode"].Value = EmployeeCodeSQL;
				SqlString DelivererSQL = Deliverer;
				_dbadapter.UpdateCommand.Parameters["@Deliverer"].Value = DelivererSQL;
				SqlString WorkSQL = Work;
				_dbadapter.UpdateCommand.Parameters["@Work"].Value = WorkSQL;
				SqlString PrioritySQL = Priority;
				_dbadapter.UpdateCommand.Parameters["@Priority"].Value = PrioritySQL;
				SqlDateTime BeginDateSQL = SqlDateTime.Null;
				if (BeginDate > DateTime.MinValue)
				{
					try
					{
						BeginDateSQL = BeginDate;
					}
					catch (Exception eBeginDate)
					{
						String BeginDateMessage = eBeginDate.Message;
						BeginDateSQL = SqlDateTime.Null;
					}
				}
				_dbadapter.UpdateCommand.Parameters["@BeginDate"].Value = BeginDateSQL;
				SqlDateTime EndDateSQL = SqlDateTime.Null;
				if (EndDate > DateTime.MinValue)
				{
					try
					{
						EndDateSQL = EndDate;
					}
					catch (Exception eEndDate)
					{
						String EndDateMessage = eEndDate.Message;
						EndDateSQL = SqlDateTime.Null;
					}
				}
				_dbadapter.UpdateCommand.Parameters["@EndDate"].Value = EndDateSQL;
				SqlInt32 StatusSQL = Status;
				_dbadapter.UpdateCommand.Parameters["@Status"].Value = StatusSQL;
				SqlDouble CompleteSQL = Complete;
				_dbadapter.UpdateCommand.Parameters["@Complete"].Value = CompleteSQL;
				SqlMoney CostSQL = Cost;
				_dbadapter.UpdateCommand.Parameters["@Cost"].Value = CostSQL;
				SqlString DescriptionSQL = Description;
				_dbadapter.UpdateCommand.Parameters["@Description"].Value = DescriptionSQL;
				Int32 returnvalue = Convert.ToInt32(_dbadapter.UpdateCommand.ExecuteScalar());

				_trans.Commit();
				return returnvalue;
			}
			catch (SqlException e)
			{
				try
				{
					_trans.Rollback();
				}
				catch (SqlException ex)
				{
					throw new Exception("SQL Rollback Error: HRM_WORK_Update: " + ex.Message, ex);
				}
				throw new Exception("SQL Error: HRM_WORK_Update: " + e.Message, e);
			}
			finally
			{
				if (_dbadapter.UpdateCommand != null)
					_dbadapter.UpdateCommand.Dispose();
				CloseConnection();
			}
		}
		public Int32 HRM_WORK_Disable(Guid WorkID, ref Boolean Enabled)
		{
			try
			{
				if (_dbconn.State != ConnectionState.Open)
					_dbconn.Open();
				_dbadapter.UpdateCommand = new SqlCommand("HRM_WORK_Disable");
				_dbadapter.UpdateCommand.CommandType = CommandType.StoredProcedure;
				_dbadapter.UpdateCommand.Connection = _dbconn;

				_trans = _dbconn.BeginTransaction();
				_dbadapter.UpdateCommand.Transaction = _trans;

				_dbadapter.UpdateCommand.Parameters.Clear();
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@WorkID", SqlDbType.UniqueIdentifier));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@enabled", SqlDbType.Bit));
				_dbadapter.UpdateCommand.Prepare();
				SqlGuid WorkIDSQL = WorkID;
				_dbadapter.UpdateCommand.Parameters["@WorkID"].Value = WorkIDSQL;
				_dbadapter.UpdateCommand.Parameters["@enabled"].Direction = ParameterDirection.Output;
				Int32 returnvalue = Convert.ToInt32(_dbadapter.UpdateCommand.ExecuteScalar());

				_trans.Commit();
				return returnvalue;
			}
			catch (SqlException e)
			{
				try
				{
					_trans.Rollback();
				}
				catch (SqlException ex)
				{
					throw new Exception("SQL Rollback Error: HRM_WORK_Disable: " + ex.Message, ex);
				}
				throw new Exception("SQL Error: HRM_WORK_Disable: " + e.Message, e);
			}
			finally
			{
				if (_dbadapter.UpdateCommand != null)
					_dbadapter.UpdateCommand.Dispose();
				CloseConnection();
			}
		}
		public Int32 HRM_WORK_Delete(Guid WorkID)
		{
			try
			{
				if (_dbconn.State != ConnectionState.Open)
					_dbconn.Open();
				_dbadapter.DeleteCommand = new SqlCommand("HRM_WORK_Delete");
				_dbadapter.DeleteCommand.CommandType = CommandType.StoredProcedure;
				_dbadapter.DeleteCommand.Connection = _dbconn;

				_trans = _dbconn.BeginTransaction();
				_dbadapter.DeleteCommand.Transaction = _trans;

				_dbadapter.DeleteCommand.Parameters.Clear();
				_dbadapter.DeleteCommand.Parameters.Add(new SqlParameter("@WorkID", SqlDbType.UniqueIdentifier));
				_dbadapter.DeleteCommand.Prepare();
				SqlGuid WorkIDSQL = WorkID;
				_dbadapter.DeleteCommand.Parameters["@WorkID"].Value = WorkIDSQL;
				Int32 returnvalue = Convert.ToInt32(_dbadapter.DeleteCommand.ExecuteScalar());

				_trans.Commit();
				return returnvalue;
			}
			catch (SqlException e)
			{
				try
				{
					_trans.Rollback();
				}
				catch (SqlException ex)
				{
					throw new Exception("SQL Rollback Error: HRM_WORK_Delete: " + ex.Message, ex);
				}
				throw new Exception("SQL Error: HRM_WORK_Delete: " + e.Message, e);
			}
			finally
			{
				if (_dbadapter.DeleteCommand != null)
					_dbadapter.DeleteCommand.Dispose();
				CloseConnection();
			}
		}
		public Int32 HRM_WORK_Save(Guid WorkID, String EmployeeCode, String Deliverer, String Work, String Priority, DateTime BeginDate, DateTime EndDate, Int32 Status, Double Complete, Decimal Cost, String Description)
		{
			try
			{
				if (_dbconn.State != ConnectionState.Open)
					_dbconn.Open();
				_dbadapter.UpdateCommand = new SqlCommand("HRM_WORK_Save");
				_dbadapter.UpdateCommand.CommandType = CommandType.StoredProcedure;
				_dbadapter.UpdateCommand.Connection = _dbconn;

				_trans = _dbconn.BeginTransaction();
				_dbadapter.UpdateCommand.Transaction = _trans;

				_dbadapter.UpdateCommand.Parameters.Clear();
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@WorkID", SqlDbType.UniqueIdentifier));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@EmployeeCode", SqlDbType.VarChar, 20));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Deliverer", SqlDbType.NVarChar, 100));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Work", SqlDbType.NVarChar, 255));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Priority", SqlDbType.NVarChar, 100));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@BeginDate", SqlDbType.DateTime));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@EndDate", SqlDbType.DateTime));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Status", SqlDbType.Int));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Complete", SqlDbType.Float));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Cost", SqlDbType.Money));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Description", SqlDbType.NVarChar, 4000));
				_dbadapter.UpdateCommand.Prepare();
				SqlGuid WorkIDSQL = WorkID;
				_dbadapter.UpdateCommand.Parameters["@WorkID"].Value = WorkIDSQL;
				SqlString EmployeeCodeSQL = EmployeeCode;
				_dbadapter.UpdateCommand.Parameters["@EmployeeCode"].Value = EmployeeCodeSQL;
				SqlString DelivererSQL = Deliverer;
				_dbadapter.UpdateCommand.Parameters["@Deliverer"].Value = DelivererSQL;
				SqlString WorkSQL = Work;
				_dbadapter.UpdateCommand.Parameters["@Work"].Value = WorkSQL;
				SqlString PrioritySQL = Priority;
				_dbadapter.UpdateCommand.Parameters["@Priority"].Value = PrioritySQL;
				SqlDateTime BeginDateSQL = SqlDateTime.Null;
				if (BeginDate > DateTime.MinValue)
				{
					try
					{
						BeginDateSQL = BeginDate;
					}
					catch (Exception eBeginDate)
					{
						String BeginDateMessage = eBeginDate.Message;
						BeginDateSQL = SqlDateTime.Null;
					}
				}
				_dbadapter.UpdateCommand.Parameters["@BeginDate"].Value = BeginDateSQL;
				SqlDateTime EndDateSQL = SqlDateTime.Null;
				if (EndDate > DateTime.MinValue)
				{
					try
					{
						EndDateSQL = EndDate;
					}
					catch (Exception eEndDate)
					{
						String EndDateMessage = eEndDate.Message;
						EndDateSQL = SqlDateTime.Null;
					}
				}
				_dbadapter.UpdateCommand.Parameters["@EndDate"].Value = EndDateSQL;
				SqlInt32 StatusSQL = Status;
				_dbadapter.UpdateCommand.Parameters["@Status"].Value = StatusSQL;
				SqlDouble CompleteSQL = Complete;
				_dbadapter.UpdateCommand.Parameters["@Complete"].Value = CompleteSQL;
				SqlMoney CostSQL = Cost;
				_dbadapter.UpdateCommand.Parameters["@Cost"].Value = CostSQL;
				SqlString DescriptionSQL = Description;
				_dbadapter.UpdateCommand.Parameters["@Description"].Value = DescriptionSQL;
				Int32 returnvalue = Convert.ToInt32(_dbadapter.UpdateCommand.ExecuteScalar());

				_trans.Commit();
				return returnvalue;
			}
			catch (SqlException e)
			{
				try
				{
					_trans.Rollback();
				}
				catch (SqlException ex)
				{
					throw new Exception("SQL Rollback Error: HRM_WORK_Save: " + ex.Message, ex);
				}
				throw new Exception("SQL Error: HRM_WORK_Save: " + e.Message, e);
			}
			finally
			{
				if (_dbadapter.UpdateCommand != null)
					_dbadapter.UpdateCommand.Dispose();
				CloseConnection();
			}
		}
		public DataSet HRM_WORK_GetList()
		{
			try
			{
				if (_dbconn.State != ConnectionState.Open)
					_dbconn.Open();
				_dbadapter.SelectCommand = new SqlCommand("HRM_WORK_GetList");
				_dbadapter.SelectCommand.CommandType = CommandType.StoredProcedure;
				_dbadapter.SelectCommand.Connection = _dbconn;
				_dbadapter.SelectCommand.Parameters.Clear();
				_dbadapter.SelectCommand.Prepare();
				_dbadapter.Fill(_data);

				return _data;
			}
			catch (SqlException e)
			{
				throw new Exception("SQL Error: HRM_WORK_GetList: " + e.Message, e);
			}
			finally
			{
				if (_dbadapter.SelectCommand != null)
					_dbadapter.SelectCommand.Dispose();
				CloseConnection();
			}
		}
		public DataSet HRM_WORK_Get(Guid WorkID)
		{
			try
			{
				if (_dbconn.State != ConnectionState.Open)
					_dbconn.Open();
				_dbadapter.SelectCommand = new SqlCommand("HRM_WORK_Get");
				_dbadapter.SelectCommand.CommandType = CommandType.StoredProcedure;
				_dbadapter.SelectCommand.Connection = _dbconn;
				_dbadapter.SelectCommand.Parameters.Clear();
				_dbadapter.SelectCommand.Parameters.Add(new SqlParameter("@WorkID", SqlDbType.UniqueIdentifier));
				_dbadapter.SelectCommand.Prepare();
				SqlGuid WorkIDSQL = WorkID;
				_dbadapter.SelectCommand.Parameters["@WorkID"].Value = WorkIDSQL;
				_dbadapter.Fill(_data);

				return _data;
			}
			catch (SqlException e)
			{
				throw new Exception("SQL Error: HRM_WORK_Get: " + e.Message, e);
			}
			finally
			{
				if (_dbadapter.SelectCommand != null)
					_dbadapter.SelectCommand.Dispose();
				CloseConnection();
			}
		}
	}
}
